# python内置
import json
import ast
import sys
# 第三方库
from PySide6.QtWidgets import QApplication, QMainWindow
from toolsui.tools import Ui_MainWindow
# 自定义库
from configs.tools_configs import HANDLE_OPERATE_TYPES
from toolserver.check_content import CheckContent
from toolserver.handles.format_data import FormatContent
from toolserver.handles.compress_data import CompressContent



class MainWindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        
        # load custom ui
        self.setupUi(self)
        self.bindFun()
        
    def bindFun(self):
        # handle content type
        self.operateType.currentTextChanged.connect(self.handleOperateType)
        # compresse content
        self.compressContent.clicked.connect(self.handleCompressContent)
        # format content
        self.formatContent.clicked.connect(self.handleFormatContent)
        pass
    
    def handleOperateType(cls,content):
        if content in HANDLE_OPERATE_TYPES:
            print(f'修改当前格式化内容为:{content}')
        else:
            print('没有此格式的数据')
    
    # _______________________________________________________________________________________________
    #
    #                                            compose
    # _______________________________________________________________________________________________
    #
    

    def handleCompressContent(cls):
        # get handle opereate type
        currentOperateType = cls.getOperateType()
        # get content
        content = cls.getOperateContent().strip()
        if len(content) == 0:
            cls.setSysLog(f'请输入正确的{currentOperateType}数据')
        # check content type is correct
        check_res = CheckContent().check(check_type=currentOperateType,content=content)
        if check_res['success']:
            compressResult = CompressContent().compress(currentOperateType,content)
            if compressResult['success']:
                # set result to gui
                cls.setOperateContent(content=compressResult['content'])
                # syslog
                operateLog = f'压缩{currentOperateType}完成'
            else:
                operateLog = f'压缩{currentOperateType}失败'
            cls.setSysLog(operateLog)
        else:
            cls.setSysLog(f'{currentOperateType}数据格式不正确')

    # _______________________________________________________________________________________________
    #
    #                                            format
    # _______________________________________________________________________________________________
    #
    
    def handleFormatContent(cls):
        # get handle opereate type
        currentOperateType = cls.getOperateType()
        # get content
        content = cls.getOperateContent().strip()
        if len(content) == 0:
            cls.setSysLog(f'请输入正确的{currentOperateType}数据')
        # check content type is correct
        check_res = CheckContent().check(check_type=currentOperateType,content=content)
        if check_res['success']:
            formatResult = FormatContent().format(currentOperateType,content)
            if formatResult['success']:
                # set result to gui
                cls.setOperateContent(content=formatResult['content'])
                # syslog
                operateLog = f'格式化{currentOperateType}完成'
            else:
                operateLog = f'格式化{currentOperateType}失败'
            cls.setSysLog(operateLog)
        else:
            cls.setSysLog(f'{currentOperateType}数据格式不正确')

    # _______________________________________________________________________________________________
    #
    #                                            combo box
    # _______________________________________________________________________________________________
    #
    
    def getOperateType(cls):
        return cls.operateType.currentText()
    
    # _______________________________________________________________________________________________
    #
    #                                            input content
    # _______________________________________________________________________________________________
    #
    
    def getOperateContent(cls):
        return cls.operateContent.toPlainText()
    
    def setOperateContent(cls,content:object):
        cls.operateContent.setPlainText(content)
        

    # _______________________________________________________________________________________________
    #
    #                                            syslog
    # _______________________________________________________________________________________________
    #
    def resetSysLog(cls):
        cls.sysLog.clear()
    
    def setSysLog(cls,content:str):
        print(content)
        cls.sysLog.appendPlainText(content)

if __name__ == "__main__":
    app = QApplication()
    window = MainWindow()
    window.show()
    # if not run, exited
    sys.exit(app.exec())
